package com.gywj.race.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.gywj.race.config.exception.BaseException;
import com.gywj.race.config.result.AjaxResultHandler;
import com.gywj.race.util.ObjectUtil;
import com.gywj.race.wxPub.WxPubUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class WxController {

    @GetMapping("/wx/auth")
    @ResponseBody
    public String wxAuth(@RequestParam("redirectUrl") String redirectUrl, HttpServletRequest request, HttpServletResponse response) {
        try {
            return WxPubUtil.getBaseUrl(redirectUrl);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/wx/getOpenIdByCode")
    @ResponseBody
    public Map<String, Object> getOpenIdByCode(@RequestParam String code) {
        try {
            Map<String, String> resultMap = Maps.newHashMap();
            JSONObject accessToken = WxPubUtil.getOpenIdByCode(code);
            System.out.println("===========accessToken:==========="+ObjectUtil.toString(accessToken));
            String openid = String.valueOf(accessToken.get("openid"));
            System.out.println("===========openid:==========="+openid);
            resultMap.put("openid", openid);
            return AjaxResultHandler.Success("success", resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @GetMapping("/wx/getTicket")
    @ResponseBody
    public Map<String, Object> getTicket() {
        try {

            JSONObject accessToken = WxPubUtil.getAccessToken();
            if (ObjectUtil.isNullObj(accessToken)) {
                throw new BaseException("获取access token 失败");
            }
            String access_token = String.valueOf(accessToken.get("access_token"));
            JSONObject ticket = WxPubUtil.getTicket(access_token);
            if (ObjectUtil.isNullObj(accessToken)) {
                throw new BaseException("获取ticket失败");
            }
            return AjaxResultHandler.Success("获取ticket成功", String.valueOf(ticket.get("ticket")));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    @GetMapping("/wx/getSignature")
    @ResponseBody
    public Map<String, Object> getPayConfig(@RequestParam("ticket") String ticket, @RequestParam("url") String url) {
        try {

            String signature = WxPubUtil.getSignature(ticket, url);
            return AjaxResultHandler.Success("获取signature成功", signature);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


}
